<template>
		<view class="login-container">
			<!-- 提示登录的图标 -->
			<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
			<!-- 登录按钮 -->
			<button type="primary" class="btn-login" @click="getUSerProfile">一键登录</button>
			<!-- <button type="primary" class="btn-login" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button> -->
			<!-- 登录提示 -->
			<view class="tips-text">登录后享受更多权益</view>
		</view>
</template>

<script>
	import {mapMutations,mapState} from 'vuex'
	export default {
		name:"my-login",
		data() {
			return {
				
			};
		},
		computed:{
			...mapState('m_user',['redirectInfo'])
		},
		methods:{
			...mapMutations('m_user',['updateUserInfo','updateToken','updateRedirectInfo']),
			getUserInfo(e){
				console.log('e',e)
				 // 判断是否获取用户的基本信息成功
				 if(e.detail.errMsg == "getUserInfo:fail auth deny") return uni.$showMsg('您取消了登录授权！')
				 
				 console.log(e.detail.userInfo)
			},
			async getUSerProfile(e){
				const res = await uni.getUserProfile({
					desc:"登录",
					success:(res)=> {
						if(res.errMsg == "getUserProfile:fail auth deny") return uni.$showMsg('您取消了登录授权！')
						console.log('11',res.userInfo)
						// 将用户的基本信息存储到vuex中
						this.updateUserInfo(res.userInfo)
						// 获取登录后的token字符串
						this.getToken(res)
					},
					fail: (res) => {
					return uni.$showMsg('您取消了登录授权')
					}
				})
					
			},
			// 调用登录接口，换区永久的token
			async getToken(info){
				console.log(info)
				// 调用微信登录接口
				const res = await uni.login().catch(err=>err)
				console.log('获取code',res)
				if(res.errMsg !== 'login:ok') return uni.$showMsg('登录失败!')
				
				// 准备参数对象
				const query = {
					code:res.code,
					encryptedData:info.encryptedData,
					iv:info.iv,
					rawData:info.rawData,
					signature:info.signature
				}
				
				// 换取token
				const {data:loginResult} = await uni.$http.post('/api/public/v1/users/wxlogin',query)
				// if(loginResult.meta.status === 400) {
				// 	uni.$showMsg('登陆成功')
					// this.updateToken("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo")
					this.updateToken("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo")
				// }
				// 判断vuex中的redirectInfo 是否为null
				// 不是null，则登录成功后，导航到对应的页面
				this.navigateBack()
				
				
			},
			// 返回登陆之前的页面
			navigateBack(){
				if(this.redirectInfo && this.redirectInfo.openType === 'switchTab'){
					uni.switchTab({
						url:this.redirectInfo.from,
						// 导航完成后，将redirectInfo对象重置为null
						complete:()=>{
							this.updateRedirectInfo(null)
						}
					})
				}
			}
			
		}
	}
</script>

<style lang="scss">
.login-container{
	// 登录盒子的样式
	height: 750rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	background-color: #f8f8f8;
	position: relative;
	overflow: hidden;
	position: relative;
	&::after{
		content: '';
		display: block;
		width: 100%;
		height: 40px;
		background-color: white;
		position: absolute;
		bottom: 0;
		left: 0;
		border-radius: 100%;
		transform: translateY(50%);
		overflow: hidden;
	}
	.btn-login{
		width: 90%;
		border-radius: 100px;
		background-color: #c00000;
		margin: 15px 0;
	}
	.tips-text{
		font-size: 12px;
		color: gray;
	}
}
</style>